package vip.ola.manage.sys.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import vip.ola.core.common.annotation.MethodLog;
import vip.ola.core.common.constant.Constant;
import vip.ola.core.common.domain.BizResponse;
import vip.ola.core.common.domain.PayResponse;
import vip.ola.core.entity.SysConfig;
import vip.ola.manage.common.ctrl.BaseController;
import vip.ola.manage.common.service.RpcCommonService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static vip.ola.core.common.constant.SysConfigConstant.TYPE_SETT;

@Controller
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/sys/config")
public class SysConfigController extends BaseController {

    @Autowired
    private RpcCommonService rpcCommonService;

    /**
     * 查询配置信息
     *
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
//        JSONObject param = getJsonParam(request);
//        String type = getStringRequired(param, "type");
//        JSONObject obj = rpcCommonService.rpcSysConfigService.getSysConfigObj(type);
        return ResponseEntity.ok(PayResponse.buildSuccess(null));
    }

    /**
     * 修改配置信息
     *
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    @MethodLog(remark = "修改配置信息")
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        String type = request.getParameter("type");
        if ("sett".equals(type)) {
            // 将金额元转成分
            handleParamAmount(param, "drawMaxDayAmount", "maxDrawAmount", "minDrawAmount", "feeLevel", "drawFeeLimit");
        }
        List<SysConfig> list = new ArrayList<>();
        Set<String> set = param.keySet();
        for (String key : set){
            list.add(new SysConfig(key, param.getString(key)));
        }
        rpcCommonService.rpcSysConfigService.updateAll(TYPE_SETT, list);
        return ResponseEntity.ok(BizResponse.buildSuccess());
    }

}